// pages/commodityMgt/index.js
import Common from '../../../common/common.js';
let common = new Common;
const app = getApp()
Page({
  data: {
    zbStatus: 0,
    ipx: '',
    orgSeq: '', //店铺id
    id: '', //商品id
    url: '',
    no: '',
    type: '',
    name: '',
    kcNum: '',
    fzNum: '',
    setKcNum: '',
    setFzNum: '',
    warningValue: '', //预警阈值
    amount: '', //可用库存数
    selType: '',
    overallData: 0, //1是整体数据经营中进去
    result: {}, //限购商品数据
    timingOnShelfTime: '',
    timingOffShelfTime: '',
    infor: '',
    shwShare: false
  },
  // 接受子组件传值 点击叉隐藏
  receiveValue: function (res) {
    this.setData({
      shwShare: false
    })
  },
  // 产品分享
  productShare(e) {
    this.setData({
      shwShare: true,
      infor: this.data.infor
    })
  },
  // 库存数
  getkc: function (e) {
    this.setData({
      kcNum: e.detail.value
    })
  },
  // 阈值数
  getfz: function (e) {
    this.setData({
      fzNum: e.detail.value
    })
  },
  getDetile() {
    let data = {
      orgSeq: this.data.orgSeq,
      goodsId: this.data.infor.goodsId,
    }
    common.requestAjax('/api/trade-service/v1/mystock/queryStockDetail', 'POST', data).then(res => {
      if (res.data.code == 1) {
        this.setData({
          zbGoodsStock: res.data.result.zbGoodsStock || 0,
          zbCurrentStock: res.data.result.zbCurrentStock || 0,
          warningValue: res.data.result.warningValue,
          amount: res.data.result.amount,
          result: res.data.result,
          timingOnShelfTime: res.data.result.timingOnShelfTime ? res.data.result.timingOnShelfTime.substring(0, 10) : '', //开始时间  
          timingOffShelfTime: res.data.result.timingOffShelfTime ? res.data.result.timingOffShelfTime.substring(0, 10) : '', //结束时间
        })
      }
    }).catch(err => {
      console.log(err);
    })

  },
  edit: function () {
    let data = {
      orgSeq: this.data.orgSeq,
      goodsId: this.data.infor.goodsId,
      amount: this.data.kcNum,
      beforeAmount: this.data.amount,
      unit: this.data.infor.unit,
      warningValue: this.data.fzNum
    }
    // 判断阈值是否输入
    if (this.data.fzNum == '') {
      console.log('没有法制')
      // fzNum kcNum
      data = {
        orgSeq: this.data.orgSeq,
        goodsId: this.data.infor.goodsId,
        amount: this.data.kcNum,
        beforeAmount: this.data.amount,
        unit: this.data.infor.unit,
        warningValue: this.data.warningValue
      }
    }
    // 判断库存是否输入
    if (this.data.kcNum == '') {
      console.log('没有库存')
      data = {
        orgSeq: this.data.orgSeq,
        goodsId: this.data.infor.goodsId,
        amount: this.data.amount,
        beforeAmount: this.data.amount,
        unit: this.data.infor.unit,
        warningValue: this.data.fzNum
      }
    }
    if (this.data.kcNum == '' && this.data.fzNum == '') {
      console.log('都没有')
      data = {
        orgSeq: this.data.orgSeq,
        goodsId: this.data.infor.goodsId,
        amount: this.data.amount,
        beforeAmount: this.data.amount,
        unit: this.data.infor.unit,
        warningValue: this.data.warningValue,
      }
    }
    data.zbStatus = this.data.zbStatus;
    data.zbPlanId = this.data.result.zbPlanId;
    data.zbStock = this.data.zbStock;
    wx.showLoading({
      title: '修改中...',
    })
    common.requestAjax('/api/trade-service/v1/mystock/updateStockDetail', 'POST', data).then(res => {
      wx.hideLoading();
      if (res.data.code == 1) {
        wx.showToast({
          title: '修改成功！', // 标题
          icon: 'success', // 图标类型，默认success
          duration: 2000 // 提示窗停留时间，默认1500ms
        })
        this.setData({
          setKcNum: this.data.kcNum,
          setFzNum: this.data.fzNum,
        });
        if (this.data.overallData == 1) {
          wx.navigateBack({
            delta: 1, // 返回上一级页面。 
          })
        } else {
          wx.redirectTo({
            url: '/pages/inventoryMgt/index?orgSeq=' + this.data.orgSeq + '&selType=' + this.data.selType,
          })
        }
      }
    }).catch(err => {
      console.log(err);
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let item = JSON.parse(options.jsonStr)
    let orgSeq = app.globalData.orgSeq;
    this.setData({
      infor: item,
      orgSeq: orgSeq,
      selType: options.selType,
      overallData: options.overallData, //1是整体数据经营中进去
    });
    console.log(this.data.infor)
    this.getDetile();
    let ipx = app.globalData.isIphoneX;
    this.setData({
      ipx: ipx
    })
  },
  //修改直播间的投放量
  getZbStock(e) {
    console.log(e)
    let num = Number(e.detail.value);
    if (Number(this.data.zbGoodsStock) < num) {
      this.setData({
        zbStatus: 1, //修改了直播的库存
        zbStock: num
      })
    } else if (Number(this.data.zbGoodsStock) > num) {
      wx.showToast({
        title: '修改后的投放量不能小于当前投放量',
        icon: "none"
      })
      this.setData({
        zbStock: this.data.result.zbGoodsStock
      })
    } else if (Number(this.data.zbGoodsStock) == num) {
      console.log('没有修改');
      this.setData({
        zbStatus: 0
      })
    }
  }
})